package cn.itcast.activiti.servlet.user;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;

import cn.itcast.activiti.common.ProcessEngineUtils;


/**
 * 查询用户需要领取的任务控制器
 * @author LEE.SIU.WAH
 * @email lixiaohua7@163.com
 * @date 2016年5月7日 上午11:29:39
 * @version 1.0
 */
@WebServlet("/queryUserClaimTask.action")
public class QueryUserClaimTaskServlet extends HttpServlet{
	private static final long serialVersionUID = 6821063917433960262L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获取用户Id
		String userId=(String) request.getSession().getAttribute("username");
		System.out.println("queryUserClaimTask");
		//创建Map集合
		
		Map<String,List<String>> maps=new HashMap<String,List<String>>();
		List<String> list=new ArrayList<String>();
		list.add("admin1");
		list.add("admin2");
		list.add("admin3");
		list.add("admin4");
		maps.put("role", list);
		
		List<String> list2=new ArrayList<String>();
		list.add("admin5");
		list.add("admin6");
		list.add("admin7");
		list.add("admin8");
		maps.put("role2", list2);
		
		
		
		
		//获取流程引擎
		ProcessEngine processEngine=ProcessEngineUtils.getProcessEngine();
		//获取任务服务
		TaskService taskService=processEngine.getTaskService();
		//创建任务查询对象
		TaskQuery taskQuery=taskService.createTaskQuery();
		
		List<Task> taskLists=new ArrayList<Task>();
		/** 迭代所有的组 */
		for(Map.Entry<String, List<String>>  entry: maps.entrySet()){
			/** 判断当前组中是否包含该用户 */
			if(entry.getValue().contains(userId)){
				 /** 调用TaskQuery中方法完成查询 (添加任务候选组作为查询条件)*/
				List<Task> groupTaskLists=taskQuery.taskCandidateGroup(entry.getKey()).list();
				if(groupTaskLists!=null && groupTaskLists.size()>0){
					taskLists.addAll(groupTaskLists);
				}
			}
		}
		
		
		
		request.setAttribute("taskLists", taskLists);
		request.getRequestDispatcher("/claimTask.jsp").forward(request, response);
	}
	
}
